﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using PayWayAPI.TenPayWay;

namespace CNVP.JShop.Web.Member
{
    public partial class Tenpay_Notify : UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //密钥
            string key = "";
            var model_Col = new BLL.CNVP_Column().GetSingle_CNVP_ColumnById(31);//  ID=31 是财富通
            if (model_Col != null)
            {
                //密钥
                key = model_Col.PayKey;
            }

			//创建PayResponseHandler实例
			MediPayResponse resHandler = new MediPayResponse(Context);

			resHandler.setKey(key);

            string errmsg = "";
			//判断签名
			if(resHandler.isTenpaySign()) 
			{
				//支付结果
				string retcode = resHandler.getParameter("retcode");

				//支付状态
				string status = resHandler.getParameter("status");

				//商户订单号
				string mch_vno = resHandler.getParameter("mch_vno");

                //商品价格
                decimal total_fee = Convert.ToDecimal(resHandler.getParameter("total_fee")) / 100;
	
				if("0".Equals(retcode)) 
				{
					//------------------------------
					//处理业务开始
					//------------------------------ 
					if(Int32.Parse(status)== 3 ) 
					{
                        //买家付款成功，注意判断订单是否重复的逻辑
                        Model.CNVP_Order model = new Model.CNVP_Order();
                        model.OrderNum = mch_vno;
                        model.OrderState = 1;//JShop系统更新为‘准备发货(到付或已付)’
                        new BLL.CNVP_Order().UpdateOrderStateByNo(model);

						//支付成功，注意判断订单是否重复的逻辑
                        errmsg += "<img src=\"../images/pay_success.gif\" />";
                        errmsg += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                        errmsg += "<font size=\"5\" color=\"blue\">支付成功!</font>";
                        errmsg += "<br/>";
                        errmsg += "<br/>";
                        errmsg += "商家定单号:" + mch_vno + "<br>金额：" + total_fee.ToString() + "元(<a href =\"OrderList.aspx\">根据此定单号进行查询</a>)" + "<br/>";
					} 
					else 
					{
                        //当做不成功处理
                        errmsg += "<img src=\"../images/pay_error .gif\" />";
                        errmsg += "<font size=\"5\" color=\"red\">支付失败!</font>";
                        errmsg += "<br/>";
                        errmsg += "<br/>";
                        errmsg += "------------------------------------------";
                        errmsg += "请在财富通中查看是否支付成功。<br>成功后与客服联系！";
                        errmsg += "------------------------------------------";
					}
		
					//------------------------------
					//处理业务完毕
					//------------------------------
				} 
				else 
				{
                    //当做不成功处理
                    errmsg += "<img src=\"../images/pay_error .gif\" />";
                    errmsg += "<font size=\"5\" color=\"red\">支付失败!</font>";
                    errmsg += "<br/>";
                    errmsg += "<br/>";
                    errmsg += "------------------------------------------";
                    errmsg += "请在财富通中查看是否支付成功。<br>成功后与客服联系！";
                    errmsg += "------------------------------------------";
				}
	
			} 
			else 
			{
                //当做不成功处理
                errmsg += "<img src=\"../images/pay_error .gif\" />";
                errmsg += "<font size=\"5\" color=\"red\">支付失败!</font>";
                errmsg += "<br/>";
                errmsg += "<br/>";
                errmsg += "------------------------------------------";
                errmsg += "请在财富通中查看是否支付成功。<br>成功后与客服联系！";
                errmsg += "------------------------------------------";
			}
            LabMSG.Text = errmsg;
		}
    }
}